import {refreshCaptcha} from "./utils.js";

const captcha_img = $("#captcha")
const register = $(".register")

captcha_img.click(() => {
    refreshCaptcha(captcha_img)
})

register.click(() => {
    const username = $("#username").val()
    const mail = $("#email").val()
    const password = $("#password").val()
    const password2 = $("#confirm_password").val()
    const captcha = $("#captcha_code").val()

    $.ajax({
        url: "http://www.woniu-news.com:8082/register.php",
        type: "POST",
        xhrFields: {
            withCredentials: true
        },
        data: {
            username,
            mail,
            password,
            password2,
            captcha
        },
        dataType: "json",
        success: (resp) => {
            if (resp.code === 200) {
                alert(resp.message)
                window.location.href = "login.html"

            } else {
                alert(resp.message)
                captcha_img.attr("src", "http://www.woniu-news.com:8082/captcha.php?t=" + Math.random())
            }
        },
        error: (resp) => {
            alert(resp.message)
            captcha_img.attr("src", "http://www.woniu-news.com:8082/captcha.php?t=" + Math.random())
        }
    })
})

// 监听回车键
$(document).ready(function () {
    $('form').on('keypress', function (event) {
        if (event.which === 13) { // 回车键
            register.click() // 触发登录按钮点击事件
        }
    })
})